package main.java.indi.zyj.sort;

public class BinarySearch {
    int binarySearch(int arr[], int size, int target)
    {
        int left = 0;
        int right = size - 1;
        while (left <= right)
        {
            int mid = (left + right) / 2;
            // int mid = left + (right - left) / 2;
            if (arr[mid] < target) {
                left = mid + 1;
            }
            else if (arr[mid] > target) {
                right = mid - 1;
            }
            else {
                return mid;
            }
        }
        return -1;
    }
}
